<?php

namespace IDock\Insurance\LIHI\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\LIHI\LIHIEnums;
use IDock\Insurance\LIHI\Response\AllLIHIResponse;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllLIHIResponse
{
    public function resolve(): Response
    {
        $body = $this->getBody();
        if (!json_decode($body)) {
            $this->setError('返回数据异常');
            return  $this;
        }
        $data = json_decode($body,true);

        if ($this->printer and  is_string($this->printer)) {
            $this->printer = new $this->printer;
        }
        $resultMess = $data['result']['resultMessage'] ?? '';
        $status = $data['result']['status'] ?? '';
        if(!$status and $resultMess) {
            $this->setError($resultMess);
            return $this;
        }
        $this->setBody($data);

        return $this;
    }

    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();

        $data = $this->getBody()['result'] ?? [];
        $renewFlag = $data['renewalFlag'] ?? 0;
        if ($renewFlag) {
            $renewalInquiryPrinter->setRenewalFlg(true);
        } else {
            $this->printer = $renewalInquiryPrinter;
            return $this;
        }
        $main = $data['tPrptMainDto'] ?? [];
        //车辆信息
        $vehicle = $data['tprpTitemCarDto'] ?? [];
        $vehiclePrinter = new VehiclePrinter();
        $vehiclePrinter->vehicleName = $vehicle['vehicleModel'] ?? '';
        $vehiclePrinter->vehicleModelCode = $vehicle['modelCode'] ?? '';
        $vehiclePrinter->power = $vehicle['power'] ?? '';
        $vehiclePrinter->vehiclePrice = $vehicle['purchasePrice'] ?? '';
        $vehiclePrinter->vehicleSeat = $vehicle['seatCount'] ?? '';
        $vehiclePrinter->vehicleTonnage = round(floatval($vehicle['tonCount'] ?? 0) * 1000,2);
        $vehiclePrinter->vehicleExhaust = $vehicle['exhaustScale'] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $vehicle['vinNo'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $vehicle['engineNo'] ?? '';

        if (strlen($vehicle['enrollDate']) == 13) {
            $vehicle['enrollDate'] = $vehicle['enrollDate'] / 1000;
        }
        $renewalInquiryPrinter->firstRegisterDate = $vehicle['enrollDate'] ? date('Y-m-d',$vehicle['enrollDate']) : '';//初登日期

        //车主
        $owner = $data['tprptCarOwnerDto'] ?? [];
        $ownerPrinter = new PremiumRelationPrinter();
        $ownerPrinter = $this->buildUserInfo($ownerPrinter,$owner);
        //投保人
        $applicant = $data['tprptApplicantDto'] ?? [];
        $applicantPrinter = new PremiumRelationPrinter();
        $applicantPrinter = $this->buildUserInfo($applicantPrinter,$applicant);

        //被保人
        $insured = $data['tprptInsuredDto'] ?? [];
        $insuredPrinter = new PremiumRelationPrinter();
        $insuredPrinter = $this->buildUserInfo($insuredPrinter,$insured);

        $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
        $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
        $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        $carShipTax = $data['prpTcarshipTaxDto'] ?? [];
        //交强险
        $renewalInquiryPrinter->ciPolicyNo = $main['mtplPolicyNo'] ?? '';
        $renewalInquiryPrinter->ciStartData = $main['mtplStartDate'] ?? '';
        $renewalInquiryPrinter->ciEndData = $main['mtplEndDate'] ?? '';

        $renewalInquiryPrinter->biPolicyNo = $main['policyNo'] ?? '';
        $renewalInquiryPrinter->biStartData = $main['startDate'] ?? '';
        $renewalInquiryPrinter->biEndData = $main['endDate'] ?? '';

        $coverageList = $data['tprpTitemKindListDto'] ?? [];
        foreach ($coverageList as $policyItem){
            if($policyItem['kindCodeMain'] == 'BZ'){
                continue;
                //跳驾意险
            }
            //商业险险种
            $coveragePrinter = new PremiumCoveragePrinter();
            $coverageNo = $policyItem['kindCodeMain'] ?? '';
            $coveragePrinter->coverageCode = array_search($coverageNo, LIHIEnums::$coverageCode);
            if (!$coveragePrinter->coverageCode) {
                continue;
            }
            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
            $coveragePrinter->amount = intval($policyItem['amountMain'] ?? '');
            $coveragePrinter->unitAmount = intval($policyItem['unitAmountMain'] ?? '');
            $coveragePrinter->premium = intval($policyItem['premiumMain'] ?? '');
            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
        }

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }

    private function buildUserInfo(PremiumRelationPrinter $user,$data)
    {
        $user->name = $data['name'] ?? '';
        $user->mobile = $data['cellPhoneNo'] ?? '';
        $user->address = $data['address'] ?? '';
        $user->certificateNo = $data['idNo'] ?? '';
        $user->certificateStartDate = $data['effectedDate'] ?? '';
        $user->certificateEndDate = $data['expiredDate'] ?? '';

        if ($data['idType'] == '54') {
            $data['idType'] = '51';
        }

        $user->certificateTypeCode = array_keys(LIHIEnums::$certificateTypeCode, $data['idType'] ?? '')[0] ?? '';

        $user->email = $data['email'] ?? '';
        $user->typeCode = $user->certificateTypeCode == '01' ? '01' : '03';

        return $user;
    }
}
